package com.bianguo.zsd.SpringBootDemo.handler;

import com.bianguo.zsd.SpringBootDemo.bean.BaseEntity;
import com.bianguo.zsd.SpringBootDemo.exception.GlobalException;
import com.bianguo.zsd.SpringBootDemo.utils.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    private BaseEntity exceptionHandler(Exception e) {
        if (e instanceof GlobalException) {
            GlobalException exception = (GlobalException) e;
            return ResultUtils.error(exception.getStatus(), exception.getMessage());
        }
        logger.info("error = {}", e.getMessage());
        return ResultUtils.error(-1, "未知错误");
    }
}
